/**
 * 
 */
package com.mobile2you.nci.android.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mobile2you.nci.android.R;
import com.mobile2you.nci.android.domain.AnnuaireGroup;
import com.mobile2you.nci.android.domain.Annuaires;

/**
 * @author JBromo
 * 
 */
public class ListAnnuaire extends BaseExpandableListAdapter {

	List<AnnuaireGroup> listItems = new ArrayList<AnnuaireGroup>();
	int type;
	public static final int TYPE_ABS = 1002;
	public static final int TYPE_ACTIVITY = 1001;
	public static final int TYPE_CENTRE = 1000;

	class AnnuaireGroupItemView extends LinearLayout {
		TextView name;

		public AnnuaireGroupItemView(Context context, AnnuaireGroup centres) {
			super(context);
			LayoutInflater.from(context).inflate(R.layout.annuaire_item_view,
					this, true);
			name = (TextView) findViewById(R.id.label_centre_item_name);
			name.setText(centres.getHeader());
		}

		private void reuse(AnnuaireGroup centres) {
			name.setText(centres.getHeader());
		}
	}

	class AnnuaireItemView extends LinearLayout {
		TextView name;

		public AnnuaireItemView(Context context, Annuaires centres) {
			super(context);
			LayoutInflater.from(context).inflate(R.layout.centre_item_view,
					this, true);
			name = (TextView) findViewById(R.id.label_centre_item_name);
			name.setText(centres.getTitle());
		}

		private void reuse(Annuaires centres) {
			name.setText(centres.getTitle());
		}
	}

	/**
	 * @param context
	 */
	public ListAnnuaire(List<Annuaires> list, int type) {
		sortAnnuaires(list, type);

	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return listItems.get(groupPosition).getItems().get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return groupPosition * childPosition;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return listItems.get(groupPosition).getItems().size();
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		Annuaires annuaires = listItems.get(groupPosition).getItems()
				.get(childPosition);
		AnnuaireItemView itemView = null;
		if (convertView != null) {
			itemView = (AnnuaireItemView) convertView;
			itemView.reuse(annuaires);
			return itemView;
		}
		return new AnnuaireItemView(parent.getContext(), annuaires);
	}

	@Override
	public Object getGroup(int groupPosition) {
		return listItems.get(groupPosition);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.ExpandableListAdapter#getGroupCount()
	 */
	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return listItems.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		isExpanded = true;
		AnnuaireGroup annuaireGroup = listItems.get(groupPosition);
		AnnuaireGroupItemView itemView = null;
		if (convertView != null) {
			itemView = (AnnuaireGroupItemView) convertView;
			itemView.reuse(annuaireGroup);
		}
		return new AnnuaireGroupItemView(parent.getContext(), annuaireGroup);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.BaseExpandableListAdapter#onGroupCollapsed(int)
	 */
	/**
	 * @return the type
	 */
	public int getType() {
		return type;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.ExpandableListAdapter#hasStableIds()
	 */
	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.ExpandableListAdapter#isChildSelectable(int, int)
	 */
	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public void onGroupCollapsed(int groupPosition) {
		// TODO Auto-generated method stub
		super.onGroupExpanded(groupPosition);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.BaseExpandableListAdapter#onGroupExpanded(int)
	 */
	@Override
	public void onGroupExpanded(int groupPosition) {
		// TODO Auto-generated method stub
		super.onGroupExpanded(groupPosition);
	}

	/**
	 * @param type
	 *            the type to set
	 */
	public void setType(int type, List<Annuaires> list) {
		this.type = type;
		sortAnnuaires(list, type);
	}

	private void sortAnnuaires(List<Annuaires> list, int type) {
		if (type == TYPE_ACTIVITY) {
			for (Annuaires annuaire : list) {
				String activity = annuaire.getActivity();
				List<Annuaires> child = new ArrayList<Annuaires>();
				for (Annuaires item : list) {
					if (item.getActivity() != null
							&& item.getActivity().equalsIgnoreCase(activity)) {
						child.add(item);
					}
				}
				AnnuaireGroup group = new AnnuaireGroup(activity, child);
				if (!listItems.contains(group)) {
					listItems.add(group);
				}
			}
		}

		if (type == TYPE_CENTRE) {
			for (Annuaires annuaire : list) {
				String centre = annuaire.getCentre();
				List<Annuaires> child = new ArrayList<Annuaires>();
				for (Annuaires item : list) {
					if (item.getCentre() != null
							&& item.getCentre().equalsIgnoreCase(centre)) {
						child.add(item);
					}
				}
				Collections.sort(child);
				AnnuaireGroup group = new AnnuaireGroup(centre, child);
				if (!listItems.contains(group)) {
					listItems.add(group);
				}
			}
		}

		if (type == TYPE_ABS) {
			for (Annuaires annuaire : list) {
				String title = annuaire.getTitle();
				String alphabet = title.substring(0, 1);
				List<Annuaires> child = new ArrayList<Annuaires>();
				for (Annuaires item : list) {
					if (item.getTitle() != null
							&& item.getTitle().toLowerCase()
									.startsWith(title.toLowerCase())) {
						child.add(item);
					}
				}
				Collections.sort(child);
				AnnuaireGroup group = new AnnuaireGroup(alphabet.toUpperCase(),
						child);
				if (!listItems.contains(group)) {
					listItems.add(group);
				}

			}
		}
		Collections.sort(listItems);
	}

}
